package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njworkorder.Entity.MaintenanceContent;
import com.njworkorder.Entity.MaintenanceMethod;
import com.njworkorder.Service.MaintenanceContentService;
import com.njworkorder.Service.MaintenanceMethodService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Tag(name = "维修方式与内容", description = "维修方式与内容相关接口")
@RestController
@RequestMapping("/maintenance")
public class MaintenanceController {

    private final MaintenanceMethodService maintenanceMethodService;
    private final MaintenanceContentService maintenanceContentService;

    public MaintenanceController(MaintenanceMethodService maintenanceMethodService, MaintenanceContentService maintenanceContentService) {
        this.maintenanceMethodService = maintenanceMethodService;
        this.maintenanceContentService = maintenanceContentService;
    }

    @Parameter(name = "methodName", description = "维修方式名称", required = true)
    @Operation(summary = "添加维修方式")
    @PostMapping("/addMethodInfo")
    public Result<String> addMethodInfo(@RequestParam("methodName") String methodName, @RequestParam("remark") String remark, @RequestParam("status") String status){
        MaintenanceMethod maintenanceMethod = new MaintenanceMethod();
        maintenanceMethod.setId(UUID.randomUUID().toString());
        maintenanceMethod.setMethodName(methodName);
        maintenanceMethod.setRemark(remark);
        maintenanceMethod.setStatus(status);
        maintenanceMethod.setPublishTime(new Date());
        boolean save = maintenanceMethodService.save(maintenanceMethod);
        if(save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Operation(summary = "根据id获取维修方式")
    @GetMapping("/getMethodInfoById")
    public Result<MaintenanceMethod> getMethodInfo(@RequestParam("id") String id){
        MaintenanceMethod maintenanceMethod = maintenanceMethodService.getById(id);
        return ResultUtil.success(maintenanceMethod);
    }

    @Operation(summary = "根据id删除维修方式")
    @GetMapping("/deleteMethodInfoById")
    public Result<String> deleteMethodInfo(@RequestParam("id") String id){
        boolean removeById = maintenanceMethodService.removeById(id);
        if(removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }

    @Operation(summary = "根据id修改维修方式")
    @Parameter(name = "methodName", description = "维修方式名称", required = true)
    @PostMapping("/updateMethodInfoById")
    public Result<String> updateMethodInfo(@RequestParam("id") String id,@RequestParam("methodName") String methodName, @RequestParam("remark") String remark, @RequestParam("status") String status){
        MaintenanceMethod maintenanceMethod = new MaintenanceMethod();
        maintenanceMethod.setId(id);
        maintenanceMethod.setMethodName(methodName);
        maintenanceMethod.setRemark(remark);
        maintenanceMethod.setStatus(status);
        boolean updateById = maintenanceMethodService.updateById(maintenanceMethod);
        if(updateById){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.error("修改失败");
    }

    @Operation(summary = "获取维修方式列表")
    @GetMapping("/getMethodInfoList")
    public Result<List<MaintenanceMethod>> getMethodInfoList(){
        LambdaQueryWrapper<MaintenanceMethod> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.apply("1=1");
        List<MaintenanceMethod> list = maintenanceMethodService.list(lambdaQueryWrapper);
        return ResultUtil.success(list);
    }

    @Operation(summary = "添加维修内容")
    @Parameter(name = "contentName", description = "维修内容名称", required = true)
    @PostMapping("/addContentInfo")
    public Result<String> addContentInfo(@RequestParam("contentName") String contentName, @RequestParam("remark") String remark, @RequestParam("status") String status){
        MaintenanceContent maintenanceContent = new MaintenanceContent();
        maintenanceContent.setId(UUID.randomUUID().toString());
        maintenanceContent.setContentName(contentName);
        maintenanceContent.setRemark(remark);
        maintenanceContent.setStatus(status);
        maintenanceContent.setPublishTime(new Date());
        boolean save = maintenanceContentService.save(maintenanceContent);
        if(save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Operation(summary = "根据id删除维修内容")
    @GetMapping("/deleteContentInfoById")
    public Result<String> deleteContentInfoById(@RequestParam("id") String id){
        boolean removeById = maintenanceContentService.removeById(id);
        if(removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }

    @Operation(summary = "根据id获取维修内容")
    @GetMapping("/getContentInfoById")
    public Result<MaintenanceContent> getContentInfoById(@RequestParam("id") String id){
        MaintenanceContent maintenanceContent = maintenanceContentService.getById(id);
        return ResultUtil.success(maintenanceContent);
    }

    @Parameter(name = "contentName", description = "维修内容名称", required = true)
    @Operation(summary = "根据id修改维修内容")
    @PostMapping("/updateContentInfoById")
    public Result<String> updateContentInfoById(@RequestParam("id") String id,@RequestParam("contentName") String contentName, @RequestParam("remark") String remark, @RequestParam("status") String status){
        MaintenanceContent maintenanceContent = new MaintenanceContent();
        maintenanceContent.setId(id);
        maintenanceContent.setContentName(contentName);
        maintenanceContent.setRemark(remark);
        maintenanceContent.setStatus(status);
        boolean updateById = maintenanceContentService.updateById(maintenanceContent);
        if(updateById){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.error("修改失败");
    }

    @Operation(summary = "获取维修内容列表")
    @GetMapping("/getContentInfoList")
    public Result<List<MaintenanceContent>> getContentInfoList(){
        LambdaQueryWrapper<MaintenanceContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.apply("1=1");
        List<MaintenanceContent> list = maintenanceContentService.list(lambdaQueryWrapper);
        return ResultUtil.success(list);
    }


}
